<div class="easyui-layout" data-options="fit:true">
	<div data-options="region:'center',title:'Main Title',iconCls:'icon-ok'">
		<div id="wu-dialog" class="easyui-dialog" data-options="closed:false,iconCls:'icon-save'" style="width:400px; padding:10px;">
			 <form id="wu-form" class="easyui-form" method="post" data-options="novalidate:true">
			    <div style="margin-bottom:20px">
			        用户名：<input class="easyui-textbox" name="name" style="width:100%" data-options="label:'用户名:',required:true">
			    </div>
			    <div style="margin-bottom:20px">
			       密码：<input class="easyui-textbox" name="password" style="width:100%" data-options="label:'密码:',required:true" id="password">
			    </div>
		 	</form> 
		</div>
	</div>
</div>

<!-- Begin of easyui-dialog -->



<!-- End of easyui-dialog -->
<script type="text/javascript">		
	//$('#wu-form').form('clear');
	$('#wu-dialog').dialog({
		closed: false,
		modal:true,		
        title: "登录/注册",
        buttons: [{
            text: '确定',
            iconCls: 'icon-ok',
            handler: login
        }, {
            text: '注册',
            iconCls: 'icon-tip',            
            handler:register
        }],
        onClose:function(){        	
        	parent.window.close();        	
        }
    });

    function login(){
    	$('#wu-dialog').form('submit',{
	        onSubmit:function(){
	            if ($(this).form('enableValidation').form('validate')){
					$.ajax({
						type:"post",
						url:"http://localhost:3000/users/list",
						data:{'name':$('#wu-form input:first').val()}
					}).then(function(res){
						console.log(res);
						if(res && res.length!=0){
							for(var i = 0;i<res.rows.length;i++){
								if(res.rows[i].password==$('#password').val()){
									$('#wu-dialog').dialog('close');
									var userId=res.rows[0]._id;
									addCookie('userId',userId,7);
									break;
								}
							}
						}else{
							alert('用户名或密码错误');
						}
					});
	            }else{
	            	alert('no');
	            }
	        }
	    });

    }

    function register(){
    	$('#wu-dialog').form('submit',{
	        onSubmit:function(){
	            if ($(this).form('enableValidation').form('validate')){
					$.ajax({
						type:"post",
						url:"http://localhost:3000/users/data",
						data:$('#wu-form').serializeJSON()
					}).then(function(res){
						alert('注册成功');
						$('#wu-dialog').datagrid('reload');
					});
	            }else{
	            	alert('no');
	            }					
	        }
	    });
    }

</script>